package com.neusoft.yuejian_01.fragment;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.neusoft.yuejian_01.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment {
    private RadioGroup radioGroup;
    private  TrainFragment trainFragment;
    private  ClubFragment clubFragment;
    private HomeFragment homeFragment;
    private  SetPictureFragment setPictureFragment;
    private  MeFragment meFragment;
    private RadioGroup.OnCheckedChangeListener mListenter;

    private FragmentManager manager;
    private FragmentTransaction transaction;
    private int page;

    public MainFragment() {
        // Required empty public constructor
    }

    public void updata(){
        Log.e("xxxxxxxxxx",page+"xxxxx");
        switch (page){
            case 0:
                break;
            case 1:
                break;
            case 2:
                break;
            case 3:
                setPictureFragment=new SetPictureFragment();
                replace(setPictureFragment);
                break;
            case 4:
                break;
        }
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);
        manager = getChildFragmentManager();
        mListenter = new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.radio_train:
                        page=0;
                        trainFragment=new TrainFragment();
                        replace(trainFragment);
                        break;
                    case  R.id.radio_club:
                        page=1;
                        clubFragment= new ClubFragment();
                        replace(clubFragment);
                        break;
                    case  R.id.radio_home:
                        page=2;
                        homeFragment=new HomeFragment();
                        replace(homeFragment);
                        break;
                    case  R.id.radio_post:
                        page=3;
                        setPictureFragment=new SetPictureFragment();
                        replace(setPictureFragment);
                        break;
                    case  R.id.radio_me:
                        page=4;
                        meFragment=new MeFragment() ;
                        replace(meFragment);
                        break;
                }
            }
        };
        radioGroup.setOnCheckedChangeListener(mListenter);
        radioGroup.check(R.id.radio_home);
    }
    private void replace(Fragment fragment) {
        transaction = manager.beginTransaction();
        transaction.replace(R.id.fragmentLayout, fragment);
        transaction.commit();
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

}
